        //
//  HomeViewController.swift
//  MicroBlog
//
//  Created by  NorthCity on 15/11/3.
//  Copyright © 2015年  NorthCity. All rights reserved.
//

import UIKit
import SVProgressHUD
class HomeViewController: VisitorViewController {
    /// 微博列表视图模型
    private var listViewModel = StatusListViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        if !UserAccountViewModel.sharedUserAccount.userLogin{
       visitorView?.setupInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
      return
    }
        
//        监听通知
        NSNotificationCenter.defaultCenter().addObserverForName(TcgStatusSelectedPhotoNotification, object: nil, queue: nil) { [weak self](n) -> Void in
            guard let urls = n.userInfo![TcgStatusSelectedPhotoURLsKey] as?[NSURL] else{
                return
            }
            guard let indexPath = n.userInfo![TcgStatusSelectedPhotoIndexPathKey]as? NSIndexPath else{
                return
            }
            guard let cell = n.object as? PictureBrowserPresentDelegate else{
                return
            }
            
            let vc = PictureBrowserViewController(urls: urls, indexPath: indexPath)
            
//            1 设置转场动画类型
              vc.modalPresentationStyle = UIModalPresentationStyle.Custom
//            2 设置转场动画代理
              vc.transitioningDelegate = self?.pictureBrowserAnimator
//            3 设置转场动画参数
              self?.pictureBrowserAnimator.setDelegateParams(cell, dismissDelegate: vc, indexPath: indexPath)
//            4 Modal 展现
            self?.presentViewController(vc, animated: true, completion: nil)
        }
        
    prepareTableView()
    loadData()
}
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
//   MARK : - 下载数据
    func loadData(){
        self.refreshControl?.beginRefreshing()
       listViewModel.loadStatus(isPullUp: pullUpView.isAnimating()){ (isSuccessed) -> () in
//        关闭刷新控件
        self.refreshControl?.endRefreshing()
        self.pullUpView.stopAnimating()
        if !isSuccessed {
            SVProgressHUD.showInfoWithStatus("您的网络不给劲")
            return
        }
//        MARK: - 输出测试数据
        self.tableView.reloadData()
        }
    }
//    MARK: - 栏加载控件  
    private lazy var pullUpView:UIActivityIndicatorView = {
        let indicator = UIActivityIndicatorView()
        indicator.backgroundColor = UIColor.lightGrayColor()
        return indicator
    }()
///   照片浏览转场动画代理
    private lazy var pictureBrowserAnimator:PictureBrowserAnimator = PictureBrowserAnimator()
//  MARK : - 准备表格
    func prepareTableView (){
//         注册 cell
        tableView.registerClass(StatusRetweetedCell.self, forCellReuseIdentifier: StatusRetweetedCellId)
        tableView.registerClass(StatusNormalCell.self, forCellReuseIdentifier: StatusNormalCellId)
//         取消分割线
        tableView.separatorStyle = .None
//         设置行高
        tableView.estimatedRowHeight = 300
//         下拉刷新控件    -  高度 60
        refreshControl = WBRefreshControl()
//         上拉刷新控件
        tableView.tableFooterView = pullUpView
//      添加监听方法
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
    }

}
// MARK: - 数据源方法
        extension HomeViewController{
            
            override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return listViewModel.statusList.count
            }
            
            override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//                1 获取数据模型
                let vm = self.listViewModel.statusList[indexPath.row]
//                2 获取可重用 cell
                let cell = tableView.dequeueReusableCellWithIdentifier(vm.cellID, forIndexPath: indexPath) as! StatusCell
//                3 设施数据模型
                cell.viewModel = vm
//                4 判定上啦刷新
                if indexPath.row == listViewModel.statusList.count - 1 && !pullUpView.isAnimating() {
                    pullUpView.startAnimating()
                    loadData()
                }
                return cell
            }
            
        }
//  MARK: - 代理方法
        extension HomeViewController{
            override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
                return listViewModel.statusList[indexPath.row].rowHeight
            }
        }
